import 'package:flutter/material.dart';
import 'dart:async';
import '../../utils/Global.dart';
class Splash extends StatefulWidget {
  Splash({Key? key}) : super(key: key);

  @override
  State<Splash> createState() => _SplashState();
}

class _SplashState extends State<Splash> {
  late Timer _timer;
  int counter = 3;
  // 倒计时
  countDown()async {
    var _duration = Duration(seconds: 1);
    Timer(_duration, () {
      // 等待1秒之后再计时
      _timer = Timer.periodic(_duration, (timer) {
        counter--;
        if(counter == 0) {
          // 执行跳转
          goHome();
        } else {
          // 不写页面不会刷新
          setState(() {
            
          });
        }
        
      });
      // _timer需要返回一下 否则接受不到
      // return _timer;
    });
  }
  void goHome() {
    _timer.cancel();
    G.router?.navigateTo(context, '/');
  }
  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    countDown();
  }
  @override
  Widget build(BuildContext context) {
    return Stack(
      alignment: Alignment(1, -1),
      children: [
        // 约束盒子 可以对子组件进行约束
        ConstrainedBox(
          // 希望子组件有个扩展的约束
          constraints: BoxConstraints.expand(),
          child: Image.asset(
            'lib/assets/images/splash.jpeg',
            fit: BoxFit.fill,
          ),
        ),
        Container(
          color: Colors.grey[350],
          // padding: EdgeInsets.fromLTRB(10, 0, 10, 0),
          margin: EdgeInsets.fromLTRB(0, 30, 20, 0),
          height: 35,
          child: TextButton(
            onPressed: () {
              goHome();
            },
            child: Text(
              '$counter跳过广告',
              style: TextStyle(
                color: Colors.white,
                fontSize: 12
              ),
            )
          ),

        )
      ],
    );
  }

   @override
    void dispose() {
      super.dispose();
    }
}